package cn.tedu.spotms.server.controller;

import cn.tedu.spotms.server.pojo.dto.MerchantSimpleDTO;
import cn.tedu.spotms.server.pojo.vo.MerchantSimpleVO;
import cn.tedu.spotms.server.service.IMerchantService;
import cn.tedu.spotms.server.web.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/merchant",produces = "application/json;charset=utf-8")
@Api(tags = "商户管理模块")
public class MerchantController {

    @Autowired
    private IMerchantService service;

    @PostMapping("/add-new-merchant")
    @PreAuthorize("hasAuthority('/merchant/update')")
    public JsonResult<Void> addNewMerchant(MerchantSimpleDTO merchantSimpleDTO){
        service.addNewMerchant(merchantSimpleDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete-merchant")
    @PreAuthorize("hasAuthority('/merchant/delete')")
    public JsonResult<Void> deleteByMerchantId(Long id){
        service.deleteByMerchantId(id);
        return JsonResult.ok();
    }

    @GetMapping("/select-merchant")
    @PreAuthorize("hasAuthority('/merchant/read')")
    public JsonResult<MerchantSimpleVO> selectByMerchantName(String name){
        MerchantSimpleVO merchantSimpleVO = service.getByName(name);
        return JsonResult.ok(merchantSimpleVO);
    }

    @PostMapping("/update-merchant")
    @PreAuthorize("hasAuthority('/merchant/update')")
    public JsonResult<Void> updateByMerchantId(Long id,MerchantSimpleDTO merchantSimpleDTO){
        service.updateByMerchantId(id, merchantSimpleDTO);
        return JsonResult.ok();
    }

    @GetMapping("/merchant-list")
    @PreAuthorize("hasAuthority('/merchant/read')")
    public JsonResult<List<MerchantSimpleVO>>  merchantList(){
        List<MerchantSimpleVO> list = service.getAllMerchant();
        return JsonResult.ok(list);
    }

}
